package com.dqp.cslggroup.ConnectSever;

import android.annotation.SuppressLint;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.AppUtils;
import com.dqp.cslggroup.Util.Comment;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Send {
    private static Connection.Response response;

    public static void join_user(String sno, String name, String grade, String faculty, String specialities) {
        new Thread(() -> {
            try {
                PackageManager pm = MyApplication.getContext().getPackageManager();
                ApplicationInfo appInfo = pm.getApplicationInfo(AppUtils.getPackageName(MyApplication.getContext()), 0);
                String appFile = appInfo.sourceDir;
                long installed = new File(appFile).lastModified();
                @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
                String day = sdf.format(new Date(installed));   // 时间戳转换成时间
                Jsoup.connect("http://210.28.164.3/47.96.190.22/app/user.php")
                        .ignoreContentType(true) // 获取图片需设置忽略内容类型
                        .method(Connection.Method.POST)
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                        .data("sno", sno)
                        .data("name", name)
                        .data("grade", grade)
                        .data("faculty", faculty)
                        .data("specialities", specialities)
                        .data("install", day)
                        .timeout(3000)
                        .execute();

            } catch (IOException | PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static boolean putComment(Comment comment, String name) {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/club/putComment.php")
                    .ignoreContentType(true) // 获取图片需设置忽略内容类型
                    .method(Connection.Method.POST)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("sno", comment.getSno())
                    .data("name", comment.getComment_name())
                    .data("gender", comment.getGender())
                    .data("content", comment.getContent())
                    //.data("img", comment.getImg())
                    .data("community", name)
                    .data("college", comment.getCollege())
                    .timeout(3000)
                    .execute();
            Document document = Jsoup.parse(response.body());
            if (document != null && document.text().contains("成功"))
                return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static boolean putTalkComment(Comment comment, String id) {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/putComment.php")
                    .ignoreContentType(true) // 获取图片需设置忽略内容类型
                    .method(Connection.Method.POST)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("sno", comment.getSno())
                    .data("name", comment.getComment_name())
                    .data("gender", comment.getGender())
                    .data("content", comment.getContent())
                    //.data("img", comment.getImg())
                    .data("college", comment.getCollege())
                    .data("id", id)
                    .timeout(3000)
                    .execute();
            Document document = Jsoup.parse(response.body());
            if (document != null && document.text().contains("成功"))
                return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static void collegePv(String name) {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/collegepv.php")
                    .ignoreContentType(true) // 获取图片需设置忽略内容类型
                    .method(Connection.Method.POST)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("name", name)
                    .timeout(3000)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }

    public static void jwxtPv() {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/apppv/jwxt.php")
                    .ignoreContentType(true) // 获取图片需设置忽略内容类型
                    .method(Connection.Method.POST)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .timeout(3000)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }

    public static boolean putClubSubmit(String name, String level, String qq, String des) {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/club/clubSubmit.php")
                    .ignoreContentType(true) // 获取图片需设置忽略内容类型
                    .method(Connection.Method.POST)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("level", level)
                    .data("qq", qq)
                    .data("des", des)
                    .data("name", name)
                    .timeout(3000)
                    .execute();
            Document document = Jsoup.parse(response.body());
            if (document != null && document.text().contains("成功"))
                return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static boolean deleteTalk(String sno, int id, String types) {
        try {

            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/deleteTalk.php")
                    .ignoreContentType(true) // 获取图片需设置忽略内容类型
                    .method(Connection.Method.POST)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("sno", sno)
                    .data("id", id + "")
                    .data("types", types)
                    .timeout(3000)
                    .execute();
            Document document = Jsoup.parse(response.body());
            if (document != null && document.text().contains("成功"))
                return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static boolean clubEvaluation(String name, String evaluation) {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/club/clubEvaluation.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("name", name)
                    .data("evaluation", evaluation)
                    .timeout(3000)
                    .execute();
            Document document = Jsoup.parse(response.body());
            if (document != null && document.text().contains("成功"))
                return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static boolean talkZan(String id, String evaluation) {
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/talkEncourage.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("id", id)
                    .data("evaluation", evaluation)
                    .timeout(3000)
                    .execute();
            Document document = Jsoup.parse(response.body());
            if (document != null && document.text().contains("成功"))
                return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
